You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Serialization Changes The migration from Newtonsoft.Json to System.Text.Json involves changes in serialization and deserialization methods which could potentially lead to issues if not handled correctly. Ensure that all data types and formats are correctly managed with the new library, especially in complex nested objects or where specific formatting is required.
Error Handling The error handling logic has been modified to use System.Text.Json. It's crucial to ensure that the error handling still correctly parses and responds to error conditions as expected.
Attribute Changes The attributes for serialization have been changed from Newtonsoft.Json attributes to System.Text.Json attributes. Verify that the new attributes (JsonPropertyName, JsonIgnore) are correctly applied and behave as expected in all scenarios, including default handling and null value handling.
Serialization Attributes The transition to System.Text.Json involves changes to serialization attributes. Ensure that these changes do not affect the serialization and deserialization of Cookie objects, particularly with respect to handling default values and null handling.
Add JsonSerializerOptions to handle potential deserialization issues
When using JsonSerializer.Deserialize to deserialize JSON data, it's a good practice to specify JsonSerializerOptions to handle potential issues like case sensitivity and missing members. This is particularly important when migrating from Newtonsoft.Json to System.Text.Json as they handle defaults differently.
Why: The suggestion addresses a potential issue with deserialization that can arise due to differences between Newtonsoft.Json and System.Text.Json. Adding JsonSerializerOptions improves robustness and compatibility, which is crucial when migrating libraries.
9
Possible issue
Add error handling for JSON deserialization to improve robustness
Consider adding error handling for the deserialization process to prevent runtime exceptions if the JSON format is incorrect.
Why: Adding error handling for JSON deserialization is crucial to prevent runtime exceptions and improve the robustness of the code. This change addresses a significant potential issue.
9
Add missing dependency versions for newer .NET frameworks to ensure compatibility
It's recommended to specify the dependency versions for System.Threading.Tasks.Extensions in the net5.0, net6.0, net7.0, and net8.0 frameworks to ensure compatibility and prevent potential runtime issues due to missing dependencies.
Why: The suggestion correctly identifies a potential issue with missing dependencies for newer .NET frameworks, which could lead to runtime issues. Adding these dependencies improves compatibility and robustness of the code.
9
Add error handling for potential serialization failures
Consider handling the case where JsonSerializer.Serialize(writer, value, options) throws an exception due to serialization issues. This can be done by wrapping the call in a try-catch block and handling the exception appropriately, possibly by logging the error or rethrowing a more informative exception.
-JsonSerializer.Serialize(writer, value, options);+try+{+ JsonSerializer.Serialize(writer, value, options);+}+catch (Exception ex)+{+ throw new JsonException("Failed to serialize value.", ex);+}
Apply this suggestion
Suggestion importance[1-10]: 8
Why: Adding error handling for serialization failures is a good practice to ensure robustness and provide informative error messages. This suggestion addresses a potential issue that could cause runtime errors.
8
Add key existence checks to prevent runtime exceptions
Validate the presence of keys in the deserialized dictionary to prevent potential KeyNotFoundException.
-Dictionary<string, object> immutableDefaultPreferences = deserializedPreferences["frozen"] as Dictionary<string, object>;-Dictionary<string, object> editableDefaultPreferences = deserializedPreferences["mutable"] as Dictionary<string, object>;+deserializedPreferences.TryGetValue("frozen", out Dictionary<string, object> immutableDefaultPreferences);+deserializedPreferences.TryGetValue("mutable", out Dictionary<string, object> editableDefaultPreferences);
Apply this suggestion
Suggestion importance[1-10]: 8
Why: Validating the presence of keys in the deserialized dictionary prevents potential KeyNotFoundException, enhancing the code's reliability and robustness.
8
Enhancement
Improve error handling in JSON enum conversion to avoid silent failures
Add error handling for cases where the JSON value does not match any enum values to prevent returning default values without notice.
-return default;+throw new JsonException($"Unknown value: {stringValue} for enum type: {typeof(TEnum)}");
Apply this suggestion
Suggestion importance[1-10]: 8
Why: Adding error handling for unknown enum values prevents silent failures and makes debugging easier, improving the overall code quality.
8
Use JsonSerializer.Deserialize for JSON parsing instead of manual handling
Replace the direct instantiation of Dictionary<string, object> and List with calls to JsonSerializer.Deserialize to leverage System.Text.Json's capabilities to handle different JSON structures and improve performance and maintainability.
Why: Using JsonSerializer.Deserialize can improve performance and maintainability by leveraging built-in capabilities for JSON parsing. However, it may require additional validation to ensure it handles all cases correctly.
7
Enhance the exception message with more detailed context
Improve the exception message in the ProcessToken method by including more context about the error, such as the current position in the JSON document, to help with debugging.
-throw new JsonException($"Unrecognized '{reader.TokenType}' token type while parsing command response.");+throw new JsonException($"Unrecognized token type '{reader.TokenType}' at position {reader.TokenStartIndex} while parsing command response.");
Apply this suggestion
Suggestion importance[1-10]: 7
Why: Improving the exception message with more context can significantly aid in debugging by providing more information about where the error occurred. This is a useful enhancement.
7
Possible bug
Ensure correct null handling in JSON serialization for SerializableLatency
Ensure that the SerializableLatency property correctly handles null values when serializing, as the JsonIgnore attribute with condition might not be sufficient.
Why: The suggestion improves the handling of null values during serialization, which is important for data integrity. However, the existing code already uses JsonIgnoreCondition.WhenWritingNull, which is generally sufficient.
7
Maintainability
Refactor ProcessToken into smaller methods for better readability and maintainability
Refactor the ProcessToken method to separate concerns and improve readability by breaking down the method into smaller, more focused methods, each handling a specific token type.
Why: Refactoring the ProcessToken method into smaller methods can improve readability and maintainability. This is a good practice but not critical for functionality.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
This is long awaited technical debt.
Description
net8.0
target framework - free of dependenciesnetstandard2.0
target framework is still dependent onSystem.Text.Json
Motivation and Context
Fixes #13097
Types of changes
Checklist
All existing tests passed:
PR Type
Enhancement, Configuration changes, Dependencies, Tests
Description
Newtonsoft.Json
withSystem.Text.Json
for serialization/deserialization across multiple classes.System.Text.Json
.net8.0
target framework and updated dependencies accordingly.net8.0
.System.Text.Json
.Changes walkthrough 📝
27 files
DevToolsSession.cs
Migrate DevToolsSession to System.Text.Json
dotnet/src/webdriver/DevTools/DevToolsSession.cs
Newtonsoft.Json
withSystem.Text.Json
forserialization/deserialization.
JsonNode
instead ofJToken
.JsonSerializer
.ResponseValueJsonConverter.cs
Update ResponseValueJsonConverter to use System.Text.Json
dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs
Newtonsoft.Json
withSystem.Text.Json
for custom JSONconverter.
Utf8JsonReader
andUtf8JsonWriter
.Proxy.cs
Update Proxy class to use System.Text.Json attributes
dotnet/src/webdriver/Proxy.cs
JsonProperty
withJsonPropertyName
andJsonIgnore
attributes.JsonObject
attribute.Cookie.cs
Update Cookie class to use System.Text.Json attributes
dotnet/src/webdriver/Cookie.cs
JsonProperty
withJsonPropertyName
andJsonIgnore
attributes.JsonObject
attribute.SeleniumManager.cs
Migrate SeleniumManager to System.Text.Json
dotnet/src/webdriver/SeleniumManager.cs
Newtonsoft.Json
withSystem.Text.Json
for JSON operations.Command.cs
Update Command class to use System.Text.Json attributes
dotnet/src/webdriver/Command.cs
Newtonsoft.Json
.JsonProperty
withJsonPropertyName
attributes.DevToolsCommandData.cs
Migrate DevToolsCommandData to System.Text.Json
dotnet/src/webdriver/DevTools/DevToolsCommandData.cs
JToken
withJsonNode
for command parameters and results.System.Text.Json
.DevToolsVersionInfo.cs
Update DevToolsVersionInfo to use System.Text.Json attributes
dotnet/src/webdriver/DevTools/DevToolsVersionInfo.cs
JsonProperty
withJsonPropertyName
attributes.JsonInclude
attributes.Response.cs
Migrate Response class to System.Text.Json
dotnet/src/webdriver/Response.cs
Newtonsoft.Json
withSystem.Text.Json
for JSON operations.FromJson
andToJson
methods.ChromiumNetworkConditions.cs
Update ChromiumNetworkConditions to use System.Text.Json attributes
dotnet/src/webdriver/Chromium/ChromiumNetworkConditions.cs
JsonProperty
withJsonPropertyName
andJsonIgnore
attributes.JsonObject
attribute.JsonEnumMemberConverter.cs
Add JsonEnumMemberConverter for System.Text.Json
dotnet/src/webdriver/DevTools/Json/JsonEnumMemberConverter.cs
System.Text.Json
.IDevToolsSession.cs
Update IDevToolsSession to use System.Text.Json
dotnet/src/webdriver/DevTools/IDevToolsSession.cs
JToken
withJsonNode
in method signatures.DomMutationData.cs
Update DomMutationData to use System.Text.Json attributes
dotnet/src/webdriver/DomMutationData.cs
JsonProperty
withJsonPropertyName
attributes.StackTraceElement.cs
Update StackTraceElement to use System.Text.Json attributes
dotnet/src/webdriver/StackTraceElement.cs
JsonProperty
withJsonPropertyName
attributes.FirefoxProfile.cs
Migrate FirefoxProfile to System.Text.Json
dotnet/src/webdriver/Firefox/FirefoxProfile.cs
Newtonsoft.Json
withSystem.Text.Json
for JSON operations.DevToolsSessionEventReceivedEventArgs.cs
Update DevToolsSessionEventReceivedEventArgs to use System.Text.Json
dotnet/src/webdriver/DevTools/DevToolsSessionEventReceivedEventArgs.cs
JToken
withJsonNode
for event data.JavaScriptEngine.cs
Migrate JavaScriptEngine to System.Text.Json
dotnet/src/webdriver/JavaScriptEngine.cs
Newtonsoft.Json
withSystem.Text.Json
for JSON operations.FirefoxExtension.cs
Update FirefoxExtension to use System.Text.Json
dotnet/src/webdriver/Firefox/FirefoxExtension.cs
JObject
withJsonNode
for JSON parsing.DriverOptions.cs
Migrate DriverOptions to System.Text.Json
dotnet/src/webdriver/DriverOptions.cs
Newtonsoft.Json
withSystem.Text.Json
for JSON serialization.RemoteSessionSettings.cs
Migrate RemoteSessionSettings to System.Text.Json
dotnet/src/webdriver/Remote/RemoteSessionSettings.cs
Newtonsoft.Json
withSystem.Text.Json
for JSON serialization.ReturnedCapabilities.cs
Migrate ReturnedCapabilities to System.Text.Json
dotnet/src/webdriver/Internal/ReturnedCapabilities.cs
Newtonsoft.Json
withSystem.Text.Json
for JSON serialization.command.hbs
Update command template for System.Text.Json
third_party/dotnet/devtools/src/generator/Templates/command.hbs
JsonProperty
withJsonPropertyName
andJsonIgnore
attributes.event.hbs
Update event template for System.Text.Json
third_party/dotnet/devtools/src/generator/Templates/event.hbs
JsonProperty
withJsonPropertyName
andJsonIgnore
attributes.type-object.hbs
Update type-object template for System.Text.Json
third_party/dotnet/devtools/src/generator/Templates/type-object.hbs
JsonProperty
withJsonPropertyName
andJsonIgnore
attributes.domain.hbs
Update domain template for System.Text.Json
third_party/dotnet/devtools/src/generator/Templates/domain.hbs
ToObject
withDeserialize
for event data.type-enum.hbs
Update type-enum template for System.Text.Json
third_party/dotnet/devtools/src/generator/Templates/type-enum.hbs
StringEnumConverter
with customJsonEnumMemberConverter
.type-hash.hbs
Update type-hash template for System.Text.Json
third_party/dotnet/devtools/src/generator/Templates/type-hash.hbs
JsonProperty
withJsonPropertyName
andJsonIgnore
attributes.1 files
ProxyTest.cs
Remove unused Newtonsoft.Json imports in ProxyTest
dotnet/test/common/ProxyTest.cs
Newtonsoft.Json
imports.9 files
TakesScreenshotTest.cs
Update TakesScreenshotTest for .NET 8.0 compatibility
dotnet/test/common/TakesScreenshotTest.cs
NET8_0
.WebDriver.IE.Tests.csproj
Update IE tests to net8.0
dotnet/test/ie/WebDriver.IE.Tests.csproj
net8.0
.WebDriver.Common.Tests.csproj
Update common tests to net8.0
dotnet/test/common/WebDriver.Common.Tests.csproj
net8.0
.WebDriver.Edge.Tests.csproj
Update Edge tests to net8.0
dotnet/test/edge/WebDriver.Edge.Tests.csproj
net8.0
.WebDriver.Firefox.Tests.csproj
Update Firefox tests to net8.0
dotnet/test/firefox/WebDriver.Firefox.Tests.csproj
net8.0
.WebDriver.Safari.Tests.csproj
Update Safari tests to net8.0
dotnet/test/safari/WebDriver.Safari.Tests.csproj
net8.0
.WebDriver.Chrome.Tests.csproj
Update Chrome tests to net8.0
dotnet/test/chrome/WebDriver.Chrome.Tests.csproj
net8.0
.WebDriver.Remote.Tests.csproj
Update Remote tests to net8.0
dotnet/test/remote/WebDriver.Remote.Tests.csproj
net8.0
.WebDriver.Support.Tests.csproj
Update Support tests to net8.0
dotnet/test/support/WebDriver.Support.Tests.csproj
net8.0
.3 files
paket.nuget.bzl
Update System.Text.Json package version
dotnet/paket.nuget.bzl
System.Text.Json
package version to 8.0.4.WebDriver.csproj
Update WebDriver.csproj for System.Text.Json
dotnet/src/webdriver/WebDriver.csproj
Newtonsoft.Json
withSystem.Text.Json
for netstandard2.0.paket.dependencies
Update System.Text.Json package version
dotnet/paket.dependencies
System.Text.Json
package version to 8.0.4.6 files
BUILD.bazel
Add net8.0 targets and update dependencies
dotnet/src/webdriver/BUILD.bazel
net8.0
framework.System.Text.Json
.WebDriver.StrongNamed.nuspec
Update nuspec for System.Text.Json and net8.0
dotnet/src/webdriver/WebDriver.StrongNamed.nuspec
Newtonsoft.Json
dependency withSystem.Text.Json
.WebDriver.nuspec
Update nuspec for System.Text.Json and net8.0
dotnet/src/webdriver/WebDriver.nuspec
Newtonsoft.Json
dependency withSystem.Text.Json
.BUILD.bazel
Update common test build for net8.0
dotnet/test/common/BUILD.bazel
net8.0
.webdriver-net8.0
.BUILD.bazel
Update support build for netstandard2.0
dotnet/src/support/BUILD.bazel
webdriver-netstandard2.0
.MODULE.bazel
Update .NET toolchain version to 8.0.203
MODULE.bazel